<template>
  <view class="safe-area-inset-bottom">
    <view class="swip_header">
      <view class="slot-box">
        <view style="width: 100%;">
          <uni-search-bar
              v-model="queryParams.params.findApi"
              type="text"
              @confirm="handleQuery"
              @input="handleQuery"
              placeholder="请输入交易编号或名称"/>
        </view>
      </view>
    </view>

    <!-- 交易明细列表 -->
    <view class="listSearchTop cardList" v-if="orderList != ''">
      <uni-card :is-shadow="false" v-for="(item,index) in orderList" :key="index">
        <p class="grid-hidden contract_Name">{{ item.name }}</p>
        <p class="grid-integral">交易编号：{{ item.number }}</p>
        <p class="grid-integral">交易渠道：
          <template v-for="(dictItem, index) in dicts.ec_trading_channel">
            <template v-if="item.channel == dictItem.value">
              <uni-tag :text="dictItem.text" size="small" :type="dictItem.type"/>
            </template>
          </template>
        </p>
        <p class="grid-integral">收支金额（元）：
          <span v-if="item.inOutType == 0" style="color: #f78989;"> - {{ item.amount }}元</span>
          <span v-else style="color: #67c23a;"> + {{ item.amount }}元</span>
        </p>
        <p class="grid-integral">账户余额（元）：{{ item.balance }}元</p>
        <p class="grid-integral">支付人：{{ item.payerName }}</p>
        <p class="grid-integral">支付时间：{{ item.newCreateTime }}</p>
        <p class="grid-integral" v-if="item.inOutType != 0">支付流水：{{ item.orderNumber }}</p>
        <view slot="actions" class="card-actions">
          <view>收支类型：
            <template v-for="(dictItem, index) in dicts.ec_change_type">
              <template v-if="item.inOutType == dictItem.value">
                <uni-tag :text="dictItem.text" size="small" :type="dictItem.type"/>
              </template>
            </template>
          </view>
          <view class="card-actions-item" @click="detail(item)" v-if="item.inOutType == 0">
            <text class="card-actions-item-text themeColor">查看详情</text>
          </view>
        </view>
      </uni-card>
      <!-- 加载更多 -->
      <uni-load-more color="#00a660" :status="loadStatus"/>
    </view>
    <!-- 暂无数据 -->
    <luanqingEmpty :show="true" text="没有更多数据啦" v-else/>
  </view>
</template>

<script>
import {listAccountBalanceRecord} from "@/api/ec/console/fee/accountBalanceRecord";
import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
import {getDictsByNameForUni} from "@/utils/dicts";
import {parseTime} from "@/utils/common";

export default {
  components: {luanqingEmpty},
  data() {
    return {
      // 数据字典
      dicts: {
        // 交易渠道
        ec_trading_channel: [],
        // 收支类型
        ec_change_type: [],
      },
      total: 0, //总条数
      loadStatus: 'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
      orderList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inOutType: null,
        channel: null,
        params: {
          findApi: undefined
        }
      },
    }
  },
  onLoad() {
    this.getList();
    this.getDicts()
  },
  /**上拉触底函数*/
  onReachBottom() {
    if (this.loadStatus != 'noMore' && this.total > this.orderList.length) {
      this.loadStatus = 'loading';
      // 延迟500毫秒显示loading效果
      setTimeout(() => {
        // 执行查询的方法
        this.getList(false);
      }, 500)
    } else {
      // 停止加载
      this.loadStatus = 'noMore'
    }
  },
  methods: {
    /**加载数据字典*/
    getDicts() {
      // 交易渠道
      this.dicts.ec_trading_channel = getDictsByNameForUni("ec_trading_channel");
      // 收支类型
      this.dicts.ec_change_type = getDictsByNameForUni("ec_change_type");
    },
    /** 余额明细列表查询 */
    getList(reload) {
      if (reload) {
        this.queryParams.pageNum = 1;
        this.loadStatus = 'more'
        this.$modal.loading();
      }
      listAccountBalanceRecord(this.queryParams).then(response => {
        if (reload) {
          uni.hideLoading()
        }
        this.total = response.total;
        if (response.total > 0) {
          const dataRows = response.rows;
          this.orderList = reload ? dataRows : this.orderList.concat(dataRows);
          this.orderList.forEach(item => {
            item.newCreateTime = parseTime(item.createTime)
          })
        } else {
          this.orderList = [];
        }
        // 判断数据是否加载完全
        if (response.total <= this.orderList.length) {
          this.loadStatus = 'noMore'
        }
        // 页数+1
        this.queryParams.pageNum++
      }).catch(() => {
        if (reload) {
          uni.hideLoading()
        }
      });
    },
    /** 查看详情 */
    detail(item) {
      uni.navigateTo({url: './detail?signFlowFeeOrder=' + JSON.stringify(item)});
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList(true);
    },
  }
}
</script>

<style scoped lang="scss">
page {
  background: $uni-bg-color-grey;
}
</style>
